package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.SessionThreadLocal;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mappper.OrdersMapper;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private OrderDetailService orderDetailService;


    @Transactional
    @Override
    public boolean submit(Orders orders) {
        //补全订单表信息
        long orderId = IdWorker.getId();
        orders.setNumber(String.valueOf(orderId));
        orders.setStatus(2);
        orders.setUserId(SessionThreadLocal.getThreadLocal());
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setPayMethod(1);

        //拿到购物车信息
        LambdaQueryWrapper<ShoppingCart> shoppingCartWrapper = new LambdaQueryWrapper<>();
        shoppingCartWrapper.eq(ShoppingCart::getUserId,SessionThreadLocal.getThreadLocal());
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(shoppingCartWrapper);

        //拿到地址信息 并封装至订单中
        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setUserName(addressBook.getConsignee());
        orders.setConsignee(addressBook.getConsignee());

        //计算总金额
        BigDecimal decimalCount = new BigDecimal("0");

        for (ShoppingCart shoppingCart : shoppingCarts) {
            //封装订单详情表
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(shoppingCart,orderDetail);
            orderDetail.setOrderId(orderId);
            //遍历订单详情 并计算出总金额
            decimalCount = decimalCount.add(orderDetail.getAmount().multiply(new BigDecimal(orderDetail.getNumber().toString())));
            //添加信息详情表于数据库中
            orderDetailService.save(orderDetail);
        }

        //将总金额封装至订单
        orders.setAmount(decimalCount);
        //将订单添加到数据库中
        this.save(orders);

        //清空购物车
        shoppingCartService.remove(shoppingCartWrapper);

        return true;
    }

    @Transactional
    @Override
    public void saveAgain(Orders orders) {
        //获取老的订单消息
        Orders oldOrders = this.getById(orders);
        //将老的信息封装到新的订单中
        BeanUtils.copyProperties(oldOrders,orders);

        //设置新的订单id未空 方便数据库存入新的id
        orders.setId(null);
        //获取一个新的订单号
        long ordersId = IdWorker.getId();

        //此时根据旧的的订单号查出所关联的订单详情表
        LambdaQueryWrapper<OrderDetail> orderWrapper = new LambdaQueryWrapper<>();
        orderWrapper.eq(OrderDetail::getOrderId,Long.parseLong(orders.getNumber()));
        List<OrderDetail> orderDetails = orderDetailService.list(orderWrapper);

        //遍历订单详情表
        orderDetails.stream().forEach(orderDetail -> {
            //赋予id空值
            orderDetail.setId(null);
            //将新的订单号赋给订单详情
            orderDetail.setOrderId(ordersId);
        });
        //赋给订单表新的订单号
        orders.setNumber(String.valueOf(ordersId));
        //设置新订单的状态
        orders.setStatus(2);
        //保存新的订单
        this.save(orders);
        //保存新的订单详情数据
        orderDetailService.saveBatch(orderDetails);
    }
}
